package textprocessing.datasources.enwikipedia;

import com.fasterxml.jackson.databind.JsonNode;
import common.dao.elastic.ElasticSearchDao;
import common.helper.json.JsonToJavaObject;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by julianzliu on 5/4/2017.
 */
public class ExtractorByQuery {


    private ElasticSearchDao esDao;
    private IndexerEnWikiNews indexer;

    public ExtractorByQuery(){
        this.indexer = new IndexerEnWikiNews();
        this.esDao = this.indexer.getEsDao();
    }



    public Long initialSearch(QueryBuilder query){
        SearchResponse response = this.esDao.searchWithApi(this.indexer.es_index, this.indexer.es_type, query, 0, 10, null, null);
        Long total_size = response.getHits().totalHits();
        return total_size;
    }


    public List<JsonNode> getDocuments(QueryBuilder query, int from, int size){
        List<JsonNode> json_hits = new ArrayList<>();
        SearchResponse response = this.esDao.searchWithApi(this.indexer.es_index, this.indexer.es_type, query, from, size, null, null);
        SearchHit[] searchHits = response.getHits().getHits();
        for(SearchHit hit: searchHits){
            String hit_str = hit.getSourceAsString();
            JsonNode hit_node = JsonToJavaObject.loadJsonNode( hit_str );
            json_hits.add( hit_node );
        }
        return json_hits;
    }

    public void traversalDocuments(QueryBuilder query, int length, int max_size){
        Long total = initialSearch( query );
        Long uppper = Math.min( max_size, total );
        int offset = 0;
        //int length = 10;
        while (offset < uppper){
            List<JsonNode>  hits = getDocuments(query, offset, length);
            System.out.println(hits.size());
            for(JsonNode jsonNode: hits){
                System.out.println( jsonNode );
            }
            offset += length;

        }
    }


    public JsonNode getById(String id){
        GetResponse response = this.esDao.getApi(this.indexer.es_index, this.indexer.es_type, id);
        String json = response.getSourceAsString();
        JsonNode node = JsonToJavaObject.loadJsonNode( json );
        return  node;
    }


    /***************************************************************
     *
     * @param args
     */
    public static void main(String[] args){
        ExtractorByQuery extractorByQuery = new ExtractorByQuery();
        QueryBuilder query = QueryBuilders.matchAllQuery();
        extractorByQuery.traversalDocuments( query, 10,100 );
    }

}
